package com.leapp.sk.ui.activity.classs

import android.os.Bundle
import android.view.View
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lklibrary.http.base.okhttpfactory.plugins.httpplugin.MediaTypeWrap
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.leapp.sk.R
import com.leapp.sk.app.CLASS_CHANGER_MANAGE
import com.leapp.sk.app.TEACHER_LIST
import com.leapp.sk.bean.TransferTecherEntity
import com.leapp.sk.bean.request.classs.ClassTransferRequestEntity
import com.leapp.sk.bean.request.classs.TeacherListRequestEntity
import com.leapp.sk.bean.response.BaseResponseDataEntity
import com.leapp.sk.bean.response.classs.TeacherListResponseEntity
import com.leapp.sk.binder.BaseBinder
import com.example.inter.ItemClickInterface
import com.leapp.sk.sever.classs.TransferTecherSever
import com.example.util.finallist.SKCODE_200
import com.leapp.sk.viewdelegate.classs.ClassManagerTransferDelegate

/**
 * 班级管理器转让
 * @author  chaohaipeng  on 2018/8/1.
 */
class ClassManagerTransferActivity :BaseActivityMVP<ClassManagerTransferDelegate>(), ItemClickInterface {
    /**
     * 点击位置
     */
    var position=0
    override fun itemClick(position: Int) {
        this.position=position
    }

    val list= mutableListOf<TransferTecherEntity>()
    override fun initDate() {
        viewDelegate.initAdapter(list,this)
        httpTeacherList()
    }

    override fun bindEvenListener() {
        viewDelegate.setOnClickListener(View.OnClickListener {
            httpTransfer(list.get(position))
        }, R.id.tv_agmt_confirm)

    }

    override fun getDelegateClass(): Class<ClassManagerTransferDelegate> {
        return ClassManagerTransferDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun initView(p0: Bundle?) {
        viewDelegate.setTitle(resources.getString(R.string.string_class_transfer))
    }
    /**
     * 转让老师列表
     */
    private fun httpTeacherList() {
        val teacherListRequestEntity = TeacherListRequestEntity()
        LKHttp.post(TEACHER_LIST, teacherListRequestEntity, TeacherListResponseEntity::class.java, object : BaseCallBack<TeacherListResponseEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: TeacherListResponseEntity?) {
                super.onSuccess(url, resultText, obj)
                if (obj?.code == SKCODE_200) {
                    val dataList = TransferTecherSever.getInstance().getDataList(obj)
                    list.addAll(dataList)
                    viewDelegate?.notifyAdapter()
                }else{
                    LKToastUtil.showToastShort(obj?.message)
                }
            }
            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , true, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))

    }

    /**
     * 转让老师列表
     */
    private fun httpTransfer(data:TransferTecherEntity) {
        val classTransferRequestEntity = ClassTransferRequestEntity()
        classTransferRequestEntity.id=data.id!!
        classTransferRequestEntity.phone=data.phone!!
        LKHttp.post(CLASS_CHANGER_MANAGE, classTransferRequestEntity, BaseResponseDataEntity::class.java, object : BaseCallBack<BaseResponseDataEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: BaseResponseDataEntity?) {
                super.onSuccess(url, resultText, obj)
                if (obj?.code == SKCODE_200) {
                    finish()
                }else{
                    LKToastUtil.showToastShort(obj?.message)
                }
            }
            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , true, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))

    }



}